/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bluetoothserver.graphical;

import bluetoothserver.dataStore.AnswerUnits;
import bluetoothserver.dataStore.Question;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Iterator;
import net.sourceforge.chart2d.*;



/**
 *
 * @author Philip
 */
public class PieChart {
    Question current;
    PieChart2D pieChart;
    int answers = 0;
    String[] answer;
    private ArrayList<AnswerUnits> results;

    public PieChart(Question q){
        current = q;
        String[] an = current.getAnswers();
        for(int i = 0;i < an.length;i++){
            if(an[i] != null){
                answers++;
            }
        }

        answer = new String[answers];
        for(int i = 0;i <answers;i++){
            answer[i] = an[i];
        }
        createChart();
    }
    
    public PieChart (Question q,ArrayList<AnswerUnits> list){
        current = q;
        results = list;
        createChart();
    }

    private void createChart(){
        pieChart = new PieChart2D();
        pieChart.setObject2DProperties(object2DProps());
        pieChart.setChart2DProperties(chart2DProps());
        pieChart.setPieChart2DProperties(pieChart2DProps());
        pieChart.setLegendProperties(legendProps());
        pieChart.setDataset(dataSet());
        pieChart.setMultiColorsProperties(multicolourProps());
        
    }

    public PieChart2D getPieChart(Dimension size){
        pieChart.setMaximumSize(size);
        return pieChart;
    }
    
    private Object2DProperties object2DProps(){
        Object2DProperties objectProps = new Object2DProperties();
        objectProps.setObjectTitleText(current.getQuestion());
        return objectProps;
    }
    
    private Chart2DProperties chart2DProps(){
        Chart2DProperties chartProps = new Chart2DProperties();
        return chartProps;
    }
    
    private PieChart2DProperties pieChart2DProps(){
        PieChart2DProperties chartProps = new PieChart2DProperties();
        chartProps.setPieLabelsExistence(true);
        return chartProps;
    }
    
    private LegendProperties legendProps(){
        LegendProperties legendProps = new LegendProperties();
        String[] list = new String[results.size()];
        Iterator<AnswerUnits> i = results.iterator();
        int j = 0;
        while(i.hasNext()){
            String s = i.next().getAnswerName();
            list[j] = s;
            j++;
        }
        legendProps.setLegendLabelsTexts(list);
        legendProps.setLegendExistence(true);
        return legendProps;
    }
    
    private Dataset dataSet(){
        Dataset dataset = new Dataset(results.size(),1,1);
            Iterator<AnswerUnits> i = results.iterator();
            int j = 0;
            while(i.hasNext()){
                
                AnswerUnits au = i.next();
                Float f  = Float.parseFloat(au.getNumberOfVotes()+"");
                dataset.set(j,0,0,f);
                j++;
            }
        return dataset;
    }
    
    private MultiColorsProperties multicolourProps(){
        MultiColorsProperties colourProps = new MultiColorsProperties();
        return colourProps;
    }
}
